package com.ashun.controller;

import com.ashun.config.JedisServer;
import com.ashun.config.RedisService;
import com.ashun.config.TntMemcachedClient;
import com.ashun.msg.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ashun.dao.UserRepository;
import com.ashun.entity.User;
import com.ashun.mapper.UserMapper;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by xufeifei on 16/9/30.
 */
@Controller
public class LoginController {
    
    //ajax是可以直接用user.,添加前缀 ，防止一个名称多个地方用到出现注入值错位
    @InitBinder("user")    
    protected void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");  
    }   

    @Autowired
    UserMapper userMapper;
    
    @Autowired
    UserRepository userRepository;

    @Autowired
    private RedisService redisService;

    @Autowired
    TntMemcachedClient tntMemcachedClient;

//    @Autowired
//    testService testService;
    @Autowired
Producer producer;
    @Autowired
    JedisServer jedisServer;

    @RequestMapping("/login")
    public String login(Model model, @ModelAttribute("user") User user1, HttpServletRequest request) {
        User user = userMapper.selectById(1);
        jedisServer.set("user",user);
        request.getSession().setAttribute("testuser",user);
        System.out.println("=====>"+request.getSession().getId());
        producer.sendMessage("kaishi");
        System.out.println("=====>>>>"+request.getSession().getAttribute("testuser"));
//        testService.firstTest();
//        User user2 = userRepository.findByUsername("111");
//        String key = "PAYMENT_DISTRIBUTION_ACTION";
//        tntMemcachedClient.set(key, user2);
//        System.out.println("1212"+tntMemcachedClient.get(key));
//        JedisUtils.getWriterInstance().setValByType("first", user, User.class, 86400);
//        model.addAttribute("user",user);
//        System.out.println(user2);
//        StringBuffer sb = new StringBuffer();
//        redisService.set("str", "str");
//        sb.append("str=").append(redisService.get("str").toString()).append(",");
//        redisService.hmSet("hmset","key","val");
//        sb.append("hmset=").append(redisService.hmGet("hmset","key")).append(",");
//        redisService.lPush("list","val");
//        sb.append("list=").append(redisService.lRange("list",0,1).toString()).append(",");
//        redisService.add("set","val");
//        sb.append("set=").append(redisService.setMembers("set").toString()).append(",");
//        redisService.zAdd("zset","val1",1);
//        redisService.zAdd("zset","val2",2);
//        sb.append("zset=").append(redisService.rangeByScore("zset",1,2)).append(",");

        return "login";
    }
}
